<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()

const currentTab = ref('home')

onMounted(() => {
  const tab = router.currentRoute.value.query.tab
  if (tab) {
    currentTab.value = tab as string
  }
})

// 为路由设置参数
const handleTabClick = (tab: string) => {
  router.push({
    path: '/',
    query: {
      tab: tab,
    },
  })
  currentTab.value = tab
}
</script>

<template>
  我是主页

  <div class="tabs">
    <div class="tab-item" :class="{ active: currentTab === 'home' }" @click="handleTabClick('home')">
      <span>首页</span>
    </div>
    <div class="tab-item" :class="{ active: currentTab === 'about' }" @click="handleTabClick('about')">
      <span>关于</span>
    </div>
  </div>
</template>

<style scoped>
.tabs {
  width: 200px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #f0f0f0;
  padding: 10px;
  border-radius: 10px;
  margin: 10px;
}

.tab-item {
  flex: 1;
  text-align: center;
  padding: 10px;
  border-radius: 10px;
  margin: 10px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.tab-item:hover {
  background-color: #e0e0e0;
}

.tab-item.active {
  background-color: #e0e0e0;
}
</style>